const db = require('../db/index');
const mysql = require('mysql');

const myDate = require('../encapsulation/tool/myDate');
const arrMethod = require('../encapsulation/tool/arrMethod');

const addDepartment = require('../encapsulation/business/attendance/02addDepartment');
const addAttendance = require('../encapsulation/business/salary/addAttendance');

// 查询公司考勤表
exports.getCompanyAttendance = (req, res) => {
	const dateStr = myDate.yearMonth(req.query.year, req.query.month);
	const sql = "select * from company_attendance where date_format(c_day_date,'%Y-%m')=?";
	db.query(sql, dateStr, (err, results) => {
		if (err) return res.cc(err);
		let specifiedDateArr = results;
		// 查询该月份上个月的数据
		let lastDate = myDate.lastDate(req.query)
		let lastDateStr = myDate.yearMonth(lastDate.year, lastDate.month)
		db.query(sql, lastDateStr, (err, results) => {
			if(err) return res.cc(err)
			let lastDateArr = results
			// 查询该月份下个月的数据
			let nextDate = myDate.nextDate(req.query)
			let nextDateStr = myDate.yearMonth(nextDate.year, nextDate.month)
			db.query(sql, nextDateStr, (err, results) => {
				if(err) return res.cc(err)
				let dateArr = [...results, ...specifiedDateArr, ...lastDateArr]
				const sql = "select * from attendance where date_format(day_date, '%Y-%m')=?"
				db.query(sql, dateStr, (err, results) => {
					if (err) return res.cc(err);
					let userArr = results;
					// 将根据用户id查出用户昵称
					const sql = 'select * from user';
					db.query(sql, (err, results) => {
						if (err) return res.cc(err);
						for (let i = 0; i < userArr.length; i++) {
							for (let j = 0; j < results.length; j++) {
								if (userArr[i].user_id == results[j].id) {
									Object.defineProperty(userArr[i], 'nick_name', {
										value: results[j].nick_name,
										writable: true,
										enumerable: true,
										configurable: true
									});
									Object.defineProperty(userArr[i], 'id_department1', {
										value: results[j].id_department1,
										writable: true,
										enumerable: true,
										configurable: true
									})
								}
							}
						}
						// 通过部门id查出部门名称
						const sql = 'select * from department1';
						db.query(sql, (err, results) => {
							if (err) return res.cc(err);
							for (let i = 0; i < userArr.length; i++) {
								for (let j = 0; j < results.length; j++) {
									if (userArr[i].id_department1 == results[j]
										.department1_id) {
										Object.defineProperty(userArr[i],
											'department_name', {
												value: results[j].department1,
												writable: true,
												enumerable: true,
												configurable: true
											})
									}
								}
							}
							res.send({
								status: 0,
								message: '查询成功',
								data: [dateArr, userArr]
							})
						})
					})
				})
			})
		})
	})
}

// 查询部门考勤表
exports.getDepartmentAttendance = (req, res) => {
	const dateStr = myDate.yearMonth(req.query.year, req.query.month);
	const sql = "select * from department_attendance where date_format(day_date, '%Y-%m')=? and department_id=?";
	db.query(sql, [dateStr, req.query.department_id], (err, results) => {
		if (err) return res.cc(err);
		let specifiedDateArr = results;
		// 查询该月份上个月的数据
		let lastDate = myDate.lastDate(req.query)
		let lastDateStr = myDate.yearMonth(lastDate.year, lastDate.month)
		db.query(sql, [lastDateStr, req.query.department_id], (err, results) => {
			if(err) return res.cc(err)
			let lastDateArr = results
			// 查询该月份下个月的数据
			let nextDate = myDate.nextDate(req.query)
			let nextDateStr = myDate.yearMonth(nextDate.year, nextDate.month)
			db.query(sql, [nextDateStr, req.query.department_id], (err, results) => {
				if(err) return res.cc(err)
				let dateArr = [...results, ...specifiedDateArr, ...lastDateArr]
				const sql = "select * from attendance where date_format(day_date, '%Y-%m')=? and department_id=?"
				db.query(sql, [dateStr, req.query.department_id], (err, results) => {
					if (err) return res.cc(err);
					let userArr = results;
					// 根据 user_id 查出用户昵称
					const sql = 'select * from user where id_department1=?';
					db.query(sql, req.query.department_id, (err, results) => {
						if (err) return res.cc(err);
						for (let i = 0; i < userArr.length; i++) {
							for (let j = 0; j < results.length; j++) {
								if (userArr[i].user_id == results[j].id) {
									Object.defineProperty(userArr[i], 'nick_name', {
										value: results[j].nick_name,
										writable: true,
										enumerable: true,
										configurable: true
									})
								}
							}
						}
						res.send({
							status: 0,
							message: '查询成功',
							data: [dateArr, userArr]
						})
					})
				})
			})
		})
	})
}

// 查询个人考勤
exports.getUserAttendance = (req, res) => {
	const dateStr = myDate.yearMonth(req.query.year, req.query.month);
	const sql = "select * from attendance where date_format(day_date, '%Y-%m')=? and user_id=?";
	db.query(sql, [dateStr, req.query.user_id], (err, results) => {
		if (err) return res.cc(err);
		let specifiedDateArr = results;
		// 查询该月份上个月的数据
		let lastDate = myDate.lastDate(req.query)
		let lastDateStr = myDate.yearMonth(lastDate.year, lastDate.month)
		db.query(sql, [lastDateStr, req.query.user_id], (err, results) => {
			if(err) return res.cc(err)
			let lastDateArr = results
			// 查询该月份下个月的数据
			let nextDate = myDate.nextDate(req.query)
			let nextDateStr = myDate.yearMonth(nextDate.year, nextDate.month)
			db.query(sql, [nextDateStr, req.query.user_id], (err, results) => {
				if(err) return res.cc(err)
				let dateArr = [...results, ...specifiedDateArr, ...lastDateArr]
				res.send({
					status: 0,
					message: '查询成功',
					data: dateArr
				})
			})
		})
	})
}

// 查询个人考勤统计
exports.getUserAttendanceStat = (req, res) => {
	const dateStr = myDate.yearMonth(req.query.year, req.query.month);
	const sql = "select * from salary where s_date=? and s_user_id=?";
	db.query(sql, [dateStr, req.query.user_id], (err, results) => {
		if (err) return res.cc(err);
		let data = results
		const sql = 'select * from user where id=?'
		db.query(sql, req.query.user_id, (err, results) => {
			if (err) return res.cc(err)
			if(data.length === 0){
				res.send({
					status: 0,
					message: '查询成功',
					data,
					nick_name: results[0].nick_name
				})
			} else {
				data[0].nick_name = results[0].nick_name
				res.send({
					status: 0,
					message: '查询成功',
					data
				})
			}
		})
	})
}

// 申请调休
exports.applyRest = (req, res) => {
	// 创建调休记录
	const sql = 'insert into rest_log set ?';
	db.query(sql, req.body, (err, results) => {
		if (err) return res.cc(err);
		const sql = 'select * from rest_log where approver_id=? and proposer_state=0'
		db.query(sql, req.body.approver_id, (err, results) => {
			if(err)return res.cc(err)
			const body = { rest_examine_num: results.length }
			const sql = 'update reddot set ? where user_id=?'
			db.query(sql, [body, req.body.approver_id], (err, results) => {
				if(err)res.cc(err)
				res.cc('申请成功，请耐心等待审核', 0);
			})
		})
	})
}

// 查询自己的调休申请记录
exports.getRestLog = (req, res) => {
	const sql = 'select * from rest_log where proposer_id=?';
	db.query(sql, req.query.user_id, (err, results) => {
		if (err) return res.cc(err);
		if (results.length < 1) return res.cc('当前没有调休记录');
		let arr = results;
		// 查询用户表,给arr添加审核人姓名字段
		const sql = 'select * from user';
		db.query(sql, (err, results) => {
			if (err) return res.cc(err);
			for(let i = 0 ; i < arr.length; i++){
				for (let j = 0 ; j < results.length; j++) {
					if (arr[i].approver_id === results[j].id){
						Object.defineProperty(arr[i], 'approver_name',{
							value: results[j].nick_name,
							writable: true,
							enumerable: true,
							configurable: true
						})
					}
				}
			}
			res.send({
				status: 0,
				message: '查询成功',
				data: arr
			});
		})
	})
}

// 查询需要自己审核的调休记录
exports.getAuditRsetLog = (req, res) => {
	const sql = 'select * from rest_log where approver_id=? order by rl_id desc';
	db.query(sql, req.query.user_id, (err, results) => {
		if (err) return res.cc(err);
		if (results.length < 1) return res.cc('当前没有调休申请');
		let arr = results;
		// 查询用户表,给arr添加申请人姓名字段
		const sql = 'select * from user';
		db.query(sql, (err, results) => {
			if (err) return res.cc(err);
			for(let i = 0 ; i < arr.length; i++){
				for (let j = 0 ; j < results.length; j++) {
					if (arr[i].proposer_id === results[j].id){
						Object.defineProperty(arr[i], 'proposer_name',{
							value: results[j].nick_name,
							writable: true,
							enumerable: true,
							configurable: true
						})
					}
				}
			}
			res.send({
				status: 0,
				message: '查询成功',
				data: arr
			});
		})
	})
}

// 拒绝调休申请
exports.refuseRest = (req, res) => {
	const sql = 'update rest_log set proposer_state=2 where rl_id=?';
	db.query(sql, req.body.rl_id, (err, results) => {
		if (err) return res.cc(err);
		const sql = 'select * from rest_log where rl_id=?'
		db.query(sql, req.body.rl_id, (err, results) => {
			if(err)return res.cc(err)
			const approver_id = results[0].approver_id
			const sql = 'select * from rest_log where approver_id=? and proposer_state=0'
			db.query(sql, approver_id, (err, results) => {
				if(err)return res.cc(err)
				const body = { rest_examine_num: results.length }
				const sql = 'update reddot set ? where user_id=?'
				db.query(sql, [body, approver_id], (err, results) => {
					if(err)res.cc(err)
					res.cc('操作成功', 0)
				})
			})
		})
	})
}

// 同意调休申请
exports.agreeRest = (req, res) => {
	// 修改审核表里的审核状态
	const sql = 'update rest_log set proposer_state=1 where rl_id=?';
	db.query(sql, req.body.rl_id, (err, results) => {
		if (err) return res.cc(err);
		// 获取审核表里的休息时间段
		const sql = 'select * from rest_log where rl_id=?';
		db.query(sql, req.body.rl_id, (err, results) => {
			if (err) return res.cc(err);
			// 处理时间段，将时间段按天分隔成数组
			let dateArr = myDate.getTimeTwo(results[0].start_date, results[0].end_date);
			// 将数组转换为对象数组
			let obj = {};
			for (let key in dateArr) {
				obj[key] = dateArr[key];
			};
			let arr = Object.keys(obj).map(val => ({
				day_date: obj[val],
				office_status: 1,
				user_id: results[0].proposer_id
			}))
			// 查出当前用户所有考勤记录
			const sql = 'select * from attendance where user_id=?';
			db.query(sql, arr[0].user_id, (err, results) => {
				if (err) return res.cc(err);
				// 给每个数组对象添加部门字段
				for (let i = 0 ; i < arr.length ; i++){
					Object.defineProperty(arr[i], 'department_id',{
						value: results[0].department_id,
						writable: true,
						enumerable: true,
						configurable: true
					})
				};
				// 将重复的部分创建为新的数组
				let updateArr = []; // 用于更新考勤表的数据
				let addArr = []; // 用于添加考勤表的数据
				for(let i = 0 ; i < arr.length ; i++){
					for(let j = 0 ; j < results.length ; j++){
						if (arr[i].day_date == results[j].day_date && arr[i].user_id == results[j].user_id){
							updateArr[i] = arr[i];
						}
					}
				}
				addArr = arrMethod.getDiffer(arr, updateArr);
				// 如果没有重复的数据，直接添加新的考勤数据
				if(updateArr.length === 0){
					// 添加考勤表
					addArr = arrMethod.mysqlArr(addArr);
					const sql = "insert into attendance(day_date, office_status, user_id, department_id) values ?";
					db.query(sql, [addArr], (err, results) => {
						if (err) return res.cc(err);
						for(let i = 0 ; i < dateArr.length ; i++){
							addDepartment.addDepartment(dateArr[i]);
						}
						const sql = 'select * from rest_log where rl_id=?'
						db.query(sql, req.body.rl_id, (err, results) => {
							if(err)return res.cc(err)
							const approver_id = results[0].approver_id
							const sql = 'select * from rest_log where approver_id=? and proposer_state=0'
							db.query(sql, approver_id, (err, results) => {
								if(err)return res.cc(err)
								const body = { rest_examine_num: results.length }
								const sql = 'update reddot set ? where user_id=?'
								db.query(sql, [body, approver_id], (err, results) => {
									if(err)res.cc(err)
									res.cc('操作成功', 0)
								})
							})
						})
					})
					return
				}
				// 更新考勤表
				let values = [];
				updateArr.forEach(function(n, i){
					values.push([n, n.day_date, n.user_id]);
				})
				const model_sql = 'update attendance set ? where day_date=? and user_id=?';
				var sqls = '';
				values.forEach((item, index) => {
					sqls += mysql.format(model_sql, item) + ';'
				})
				// 如果新添加的数据没东西，直接走更新考勤统计流程
				if (addArr.length === 0) {
					for(let i = 0 ; i < dateArr.length ; i++){
						addDepartment.addDepartment(dateArr[i]);
					}
					const sql = 'select * from rest_log where rl_id=?'
					db.query(sql, req.body.rl_id, (err, results) => {
						if(err)return res.cc(err)
						const approver_id = results[0].approver_id
						const sql = 'select * from rest_log where approver_id=? and proposer_state=0'
						db.query(sql, approver_id, (err, results) => {
							if(err)return res.cc(err)
							const body = { rest_examine_num: results.length }
							const sql = 'update reddot set ? where user_id=?'
							db.query(sql, [body, approver_id], (err, results) => {
								if(err)res.cc(err)
								res.cc('操作成功', 0)
							})
						})
					})
				} else {
					db.query(sqls, (err, results) => {
						if (err) return res.cc(err);
						// 添加考勤表
						addArr = arrMethod.mysqlArr(addArr);
						const sql = "insert into attendance(day_date, office_status, user_id, department_id) values ?";
						db.query(sql, [addArr], (err, results) => {
							if (err) return res.cc(err);
							for(let i = 0 ; i < dateArr.length ; i++){
								addDepartment.addDepartment(dateArr[i]);
							}
							const sql = 'select * from rest_log where rl_id=?'
							db.query(sql, req.body.rl_id, (err, results) => {
								if(err)return res.cc(err)
								const approver_id = results[0].approver_id
								const sql = 'select * from rest_log where approver_id=? and proposer_state=0'
								db.query(sql, approver_id, (err, results) => {
									if(err)return res.cc(err)
									const body = { rest_examine_num: results.length }
									const sql = 'update reddot set ? where user_id=?'
									db.query(sql, [body, approver_id], (err, results) => {
										if(err)res.cc(err)
										res.cc('操作成功', 0)
									})
								})
							})
						})
					})
				}
			});
		})
	})
}

// 申请加班
exports.applyOvertime = (req, res) => {
	const sql = 'insert into overtime_log set ?';
	db.query(sql, req.body, (err, results) => {
		if (err) return res.cc(err);
		const sql = 'select * from overtime_log where approver_id=? and proposer_state=0'
		db.query(sql, req.body.approver_id, (err, results) => {
			if(err)return res.cc(err)
			const body = { overtime_examine_num: results.length }
			const sql = 'update reddot set ? where user_id=?'
			db.query(sql, [body, req.body.approver_id], (err, results) => {
				if(err)res.cc(err)
				res.cc('申请成功，请耐心等待审核', 0);
			})
		})
	})
}

// 查询自己的加班申请
exports.getOvertimeLog = (req, res) => {
	const sql = 'select * from overtime_log where proposer_id=?';
	db.query(sql, req.query.user_id, (err, results) => {
		if (err) return res.cc(err);
		if (results.length < 1) return res.cc('当前没有加班记录');
		let arr = results;
		// 查询用户表,给arr添加审核人姓名字段
		const sql = 'select * from user';
		db.query(sql, (err, results) => {
			if (err) return res.cc(err);
			for(let i = 0 ; i < arr.length; i++){
				for (let j = 0 ; j < results.length; j++) {
					if (arr[i].approver_id === results[j].id){
						Object.defineProperty(arr[i], 'approver_name',{
							value: results[j].nick_name,
							writable: true,
							enumerable: true,
							configurable: true
						})
					}
				}
			}
			res.send({
				status: 0,
				message: '查询成功',
				data: arr
			});
		})
	})
}

// 查询需要自己审核的加班记录
exports.getAuditOvertimeLog = (req, res) => {
	const sql = 'select * from overtime_log where approver_id=? order by ol_id desc';
	db.query(sql, req.query.user_id, (err, results) => {
		if (err) return res.cc(err);
		if (results.length < 1) return res.cc('当前没有加班申请');
		let arr = results;
		// 查询用户表,给arr添加申请人姓名字段
		const sql = 'select * from user';
		db.query(sql, (err, results) => {
			if (err) return res.cc(err);
			for(let i = 0 ; i < arr.length; i++){
				for (let j = 0 ; j < results.length; j++) {
					if (arr[i].proposer_id === results[j].id){
						Object.defineProperty(arr[i], 'proposer_name',{
							value: results[j].nick_name,
							writable: true,
							enumerable: true,
							configurable: true
						})
					}
				}
			}
			res.send({
				status: 0,
				message: '查询成功',
				data: arr
			});
		})
	})
}

// 拒绝加班申请
exports.refuseOvertime = (req, res) => {
	const sql = 'update overtime_log set proposer_state=2 where ol_id=?';
	db.query(sql, req.body.ol_id, (err, results) => {
		if (err) return res.cc(err);
		const sql = 'select * from overtime_log where ol_id=?'
		db.query(sql, req.body.ol_id, (err, results) => {
			if(err)return res.cc(err)
			const approver_id = results[0].approver_id
			const sql = 'select * from overtime_log where approver_id=? and proposer_state=0'
			db.query(sql, approver_id, (err, results) => {
				if(err)return res.cc(err)
				const body = { overtime_examine_num: results.length }
				const sql = 'update reddot set ? where user_id=?'
				db.query(sql, [body, approver_id], (err, results) => {
					if(err)res.cc(err)
					res.cc('操作成功', 0)
				})
			})
		})
	})
}

// 同意加班申请
exports.agreeOvertime = (req, res) => {
	// 修改审核表里的审核状态
	const sql = 'update overtime_log set proposer_state=1 where ol_id=?';
	db.query(sql, req.body.ol_id, (err, results) => {
		if (err) return res.cc(err);
		// 查询提取加班表里需要的信息字段
		const sql = 'select * from overtime_log where ol_id=?';
		db.query(sql, req.body.ol_id, (err, results) => {
			if (err) return res.cc(err);
			let obj = results.map((item) => {
				return Object.assign(
					{},
					{ day_date: item.overtime_date },
					{ overtime_status: 1},
					{ overtime_time: item.overtime_hour },
					{ user_id: item.proposer_id }
				)
			})
			// 更新考勤表
			const sql = 'update attendance set ? where day_date=? and user_id=?';
			db.query(sql, [obj[0], obj[0].day_date, obj[0].user_id], (err, results) => {
				if (err) return res.cc(err);
				// 执行更新考勤统计的方法
				addDepartment.addDepartment(obj[0].day_date);
				const sql = 'select * from overtime_log where ol_id=?'
				db.query(sql, req.body.ol_id, (err, results) => {
					if(err)return res.cc(err)
					const approver_id = results[0].approver_id
					const sql = 'select * from overtime_log where approver_id=? and proposer_state=0'
					db.query(sql, approver_id, (err, results) => {
						if(err)return res.cc(err)
						const body = { overtime_examine_num: results.length }
						const sql = 'update reddot set ? where user_id=?'
						db.query(sql, [body, approver_id], (err, results) => {
							if(err)res.cc(err)
							res.cc('操作成功', 0)
						})
					})
				})
			});
		})
	})
}

// 更新初始化
exports.updateInitial = (req, res) => {
	// 修改初始化表
	const sql = 'update initial set ? where i_id=1';
	db.query(sql, req.body, (err, results) => {
		if (err) return res.cc(err);
		// 更新工资表
		addAttendance.addAttendance();
		// 设置工资表调休时间的默认值
		const sql = "ALTER TABLE `salary` CHANGE COLUMN `s_rest_all_day` `s_rest_all_day` INT NOT NULL DEFAULT ?";
		db.query(sql, req.body.rest_day, (err, results) => {
			if (err) return res.cc(err);
			res.cc('更新成功，考勤初始化将在下次考勤调整生效', 0);
		})
	})
}

// 节假日设置
exports.holidaysRest = (req, res) => {
	// 格式化日期
	const holiday_start_date = myDate.jsDateFormatter(req.body.holiday_start_date).substring(0,10); // 开始日期
	const holiday_end_date = myDate.jsDateFormatter(req.body.holiday_end_date).substring(0,10); // 结束日期
	const s_rest_all_day = req.body.holiday_rest_day; // 调休天数
	// 添加到节假日表中 insert into overtime_log set ?
	const sql = 'insert into holidays_log set ?';
	db.query(sql, req.body, (err, results) => {
		if (err) return res.cc(err);
		// 处理开始日期和结束日期，将日期分隔为时间数组
		const dateArr = myDate.getTimeTwo(holiday_start_date, holiday_end_date);
		// 将数组转换为含有日期，上班状态的对象数组 officeArr
		let obj = {};
		for (let key in dateArr) { obj[key] = dateArr[key]; };
		let officeArr = Object.keys(obj).map(val => ({
			day_date: obj[val],
			office_status: 1,
		}))
		// 查询用户列表，整理出用户id和部门id
		const sql = 'select * from user where state < 2';
		db.query(sql, (err, results) => {
			if (err) return res.cc(err);
			if (results.length === 0) return res.cc('公司没人了', 1);
			// 获取用户考勤表需要的信息，得到含用户id和部门id的数组 userArr
			let userArr = results.map((item) => {
				return Object.assign(
					{},
					{ user_id: item.id },
					{ department_id: item.id_department1}
				)
			})
			// 将 officeArr 和 userArr 相乘 得到要添加到考勤表的初始数据 arr
			let index = 0;
			let arr = [];
			for (let i = 0; i < officeArr.length; i++) {
				for (let j = 0; j < userArr.length; j++) {
					arr[index] = {...officeArr[i], ...userArr[j]};
					index++;
				}
			}
			// 根据用户传入的时间段 查询考勤表中重复的数据 attendanceArr
			const sql = "select * from attendance where day_date between'" + holiday_start_date + "' and '" + holiday_end_date +"'";
			db.query(sql, (err, results) => {
				if (err) return res.cc(err);
				let attendanceArr = results.map((item) => {
					return Object.assign(
						{},
						{ day_date: item.day_date },
						{ office_status: item.office_status },
						{ user_id: item.user_id },
						{ department_id: item.department_id}
					)
				})
				// 如果该日期内没有重复的数据，则直接添加到考勤表中
				if (attendanceArr.length === 0){
					// 将 arr 处理成 mysql可执行的格式
					arr = arrMethod.mysqlArr(arr);
					const sql = "insert into attendance(day_date, office_status, user_id, department_id) values ?";
					db.query(sql, [arr], (err, results) => {
						if (err) return res.cc(err);
						// 查询初始化天数，获取本轮考勤年月
						const sql = 'select * from initial';
						db.query(sql, (err, results) => {
							if (err) return res.cc(err);
							const init_day = results[0].init_day;
							// 本轮考勤年月
							const s_date = myDate.salary_date(init_day);
							// 修改对应考勤年月的调休时间
							const sql = 'update salary set s_rest_all_day=' + s_rest_all_day + ' where s_date=?';
							db.query(sql, s_date, (err, results) => {
								if (err) return res.cc(err);
								// 循环执行添加部门考勤表
								for(let i = 0 ; i < dateArr.length ; i++){
									addDepartment.addDepartment(dateArr[i]);
								}
								res.cc('设置成功',0)
							})
						})
					});
				} else {
					// 对比重复的数据，将需要更新的数据用 updateArr 接收,需要添加的数据 用 addArr 接收
					let updateArr = [];
					let updateArrLength = 0;
					let addArr = [];
					let addArrLength = 0;
					for(let i = 0; i < arr.length; i++){
						for(let j = 0; j < attendanceArr.length; j++){
							if(arr[i].day_date == attendanceArr[j].day_date && arr[i].user_id == attendanceArr[j].user_id){
								updateArr[updateArrLength] = arr[i];
								updateArrLength++;
							} else {
								addArr[addArrLength] = arr[i];
								addArrLength++;
							}
						}
					};
					// 更新考勤表
					let values = [];
					updateArr.forEach(function(n, i){
						values.push([n, n.day_date, n.user_id]);
					})
					const model_sql = 'update attendance set ? where day_date=? and user_id=?';
					var sqls = '';
					values.forEach((item, index) => {
						sqls += mysql.format(model_sql,item) + ';'
					})
					// 添加考勤表
					// 将 addArr 处理成 mysql可执行的格式
					addArr = arrMethod.mysqlArr(addArr);
					const sql = "insert into attendance(day_date, office_status, user_id, department_id) values ?";
					db.query(sql, [addArr], (err, results) => {
						// 查询初始化天数，获取本轮考勤年月
						const sql = 'select * from initial';
						db.query(sql, (err, results) => {
							if (err) return res.cc(err);
							const init_day = results[0].init_day;
							// 本轮考勤年月
							const s_date = myDate.salary_date(init_day);
							// 修改对应考勤年月的调休时间
							const sql = 'update salary set s_rest_all_day=' + s_rest_all_day + ' where s_date=?';
							db.query(sql, s_date, (err, results) => {
								if (err) return res.cc(err);
								// 循环执行添加部门考勤表
								for(let i = 0 ; i < dateArr.length ; i++){
									addDepartment.addDepartment(dateArr[i]);
								}
								res.cc('设置成功',0)
							})
						})
					});
				}
			});
		});
	})
}

// 获取考勤初始化
exports.getInitial = (req, res) => {
	const sql = 'select * from initial'
	db.query(sql, (err, results) => {
		if (err) return res.cc(err)
		res.send({
			status: 0,
			data: results,
			message: '查询成功'
		})
	})
}

// 查询具备审核权限的人员列表
exports.getExamineUserList = (req, res) => {
	let authorityStr = ''
	if(req.query.type == 1){
		authorityStr = '请假审批'
	} else {
		authorityStr = '加班审批'
	}
	const sql = 'select * from authority'
	db.query(sql, (err, results) => {
		if(err) return res.cc(err)
		let infoArr = []
		let authorityIds = []
		results.map(item => {
			infoArr = item.a_info.split('@')
			infoArr.map(infoItem => {
				if(infoItem === authorityStr){
					authorityIds.push(item.authority_id)
				}
			})
		})
		const sql = 'select * from user where state<2'
		db.query(sql, (err, results) => {
			if(err) return res.cc(err)
			let data = []
			results.map(item => {
				authorityIds.map(authorityIdItem => {
					if(item.id_authority == authorityIdItem){
						data.push(item)
					}
				})
			})
			res.send({
				status: 0,
				message: '查询成功',
				data
			})
		})
	})
}